package com.winteam.Controler;

import com.winteam.Bean.RoomType;
import com.winteam.dao.DbConnection;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Updata {

    private Connection con;
    private PreparedStatement ps;
    private ResultSet rs;
    //修改房间信息的
    public boolean updateRoomType(RoomType r){
        //准备sql
        String sql="update  r_tpye_info set r_type=? ,r_price = ?,r_hprice = ? ,r_bednumber =? where id=? ";

        try {
             con = DbConnection.getConnection();
             ps = con.prepareStatement(sql);
             ps.setString(1,r.getRoom_type());
             ps.setInt(2,r.getR_price());
             ps.setInt(3,r.getR_hprice());
             ps.setInt(4,r.getR_bed());
             ps.setInt(5,r.getId());
             return ps.executeUpdate()>1? false:true;
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            DbConnection.colseAll(rs,ps,con);
        }
        return false;
    }
    public int addData(String s) {
        return 1;
    }

    //添加房间类型
    public boolean AddRoomType(RoomType r){
        //准备sql
        String sql ="insert into r_tpye_info VALUES (null, ?, ?, ?,?)";
        try {
            con = DbConnection.getConnection();
            ps = con.prepareStatement(sql);
   ;
            ps.setString(1,r.getRoom_type());
            ps.setInt(2,r.getR_price());
            ps.setInt(3,r.getR_hprice());
            ps.setInt(4,r.getR_bed());

            return ps.executeUpdate()>1? false:true;
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            DbConnection.colseAll(rs,ps,con);
        }
        return false;
    }
    //删除房间的方法
    public boolean deleteRoomInfoByRoomNumer(String number){
        //准备sql
        String sql="delete from room_info where r_rommber = '"+number+"'";

        try {
            return DbConnection.updataInfo(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    //添加房间的方法
    public boolean addRomminf(int roomId, int state, String text) {
        String sql="insert into room_info VALUES(null,"+roomId+","+state+",'"+text+"')";
        try {
            return DbConnection.updataInfo(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 修改房间信息的方法
     *  根据房间号码来修该房间信息
     * @param roomId 房间类型id
     * @param r_state 房间状态
     * @param text 修改后的房间号码
     * @param roomname 房间号码
     * @return 是否修改成功
     */
    public boolean updateRoomInfoByroomname(int roomId, int r_state, String text, String roomname) {
        String sql="update room_info  set r_type = "+roomId+" ,r_state = "+ r_state+" ,r_rommber ='"+text+"' where r_rommber='"+roomname+"';";
        try {
            return DbConnection.updataInfo(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    @Test
    public void test1(){
        System.out.println(new Updata().updateAdminByName("smialyellow"));
    }
    //删除账户的方法
    public boolean updateAdminByName(String deletName) {
        String sql=" delete from account where username ='"+deletName+"'";
        try {
            return DbConnection.updataInfo(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    //修改管理密码
    public boolean updaAdminPwd(String username, String pwd1,String adminType) {
        String sql ="update account set adminnmae ='"+adminType+"' ,`password` ='"+pwd1+"' where username = '"+username+"'";
        try {
            return DbConnection.updataInfo(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    //添加管理员
    public boolean Addadmin(String username, String pwd1, String a_type) {
        String sql ="insert into account values(null,'"+username+"','"+pwd1+"','"+a_type+"');";
        try {
            return DbConnection.updataInfo(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    //添加结账单的shuju
    public boolean addChecOut(String ddh,int clinedid,int day,int moenty,String ccy,String reamk){
        String sql ="insert into balance values('"+ddh+"',"+clinedid+","+day+","+moenty+",now(),'"+ccy+"','"+reamk+"');";
        try {
            return DbConnection.updataInfo(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    //修改用户的状态为离店
    public boolean live(int id ){
        String sql ="update check_in set c_state = 2 where id = "+id+"";
        try {
            return DbConnection.updataInfo(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
    /**
     * 修改房间的状态
     */
    public boolean changeRoomStaate(String roomId){
        String sql = "update room_info set r_state =1 where r_rommber ='"+roomId+"';";
        try {
            return DbConnection.updataInfo(sql);
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
}
